using System;
using System.Threading;
using System.Windows.Forms;
using LicenseManager;

namespace ICP_MS.iMass;

internal static class Program
{
	public static string InstrumentName = "ICP-MS";

	public static int InstrumentStyle = 2;

	[STAThread]
	private static void Main()
	{
		Application.EnableVisualStyles();
		Application.SetCompatibleTextRenderingDefault(defaultValue: false);
		bool CreateNew;
		using (new Mutex(initiallyOwned: true, Application.ProductName, out CreateNew))
		{
			if (CreateNew)
			{
				using (FormLogin form = new FormLogin())
				{
					if (form.ShowDialog() == DialogResult.Cancel)
					{
						return;
					}
				}
				Application.Run(new FormMain());
			}
			else
			{
				MessageBox.Show("应用程序已经在运行中...");
				Thread.Sleep(1000);
				Environment.Exit(1);
			}
		}
	}

	public static bool CheckLicense()
	{
		DateTime validtime = EncryptManager.GetValidDate();
		DateTime lastTime = EncryptManager.GetLastTime();
		if (lastTime > validtime)
		{
			MessageBox.Show(Lang.Txt("FormRegister.Register.TimeInvalid.Text "));
			return false;
		}
		if (DateTime.Now > validtime)
		{
			EncryptManager.SaveLastTime(DateTime.Now);
		}
		int verifyResult = EncryptManager.CheckLicense(InstrumentStyle);
		bool registryResult = EncryptManager.Checkregistry();
		if (verifyResult == -1)
		{
			MessageBox.Show(Lang.Txt("Program.Register.Error.TimeInvalid.Text "));
			return false;
		}
		if (verifyResult == 1 && registryResult)
		{
			return true;
		}
		if (verifyResult == 1 && !registryResult)
		{
			MessageBox.Show(Lang.Txt("Program.Register.Error.UnRegistered.Text "));
			return false;
		}
		MessageBox.Show("识别错误，计算机唯一标识地址不正确！");
		return false;
	}
}
